package com.jcode222.config;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shardingsphere.encrypt.spi.QueryAssistedEncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.context.EncryptContext;

import java.util.Properties;

public class MD5WrapperEncryptAlgorithm implements QueryAssistedEncryptAlgorithm<Object, String> {

    private Properties props = new Properties();

    @Override
    public String queryAssistedEncrypt(Object plainValue, EncryptContext encryptContext) {
        if(null == plainValue) {
            return "";
        }
        return encrypt(plainValue, encryptContext);
    }

    @Override
    public String encrypt(Object plainValue, EncryptContext encryptContext) {
        if(null == plainValue){
            return "";
        }
        return DigestUtils.md5Hex(plainValue.toString());
    }

    @Override
    public Object decrypt(String cipherValue, EncryptContext encryptContext) {
        return cipherValue;
    }

    @Override
    public void init() {

    }

    @Override
    public String getType() {
        return "MD5Query";
    }
}
